﻿using System;
using System.Collections.Generic;
using System.Text;
using Dapper_Core.Model;
using Microsoft.AspNetCore.Http;
using Newtonsoft.Json;

namespace Dapper_Core.Common
{
    public class SessionUtility
    {
        /// <summary>
        /// 保存Session以确定SessionId
        /// </summary>
        public static void InitialSession()
        {
            SessionMemoryUtility.SetSessionData("SessionId", MyHttpContext.Current.Session.Id);
        }

        /// <summary>
        /// 获取用户基本信息
        /// </summary>
        /// <returns></returns>
        public static BasicUserInfo GetBasicUserInfo()
        {
            if (string.Equals(Appsettings.SessionType, "redis", StringComparison.OrdinalIgnoreCase))
            {
                return SessionRedisUtility.GetBasicUserInfo();
            }
            return SessionMemoryUtility.GetBasicUserInfo();
        }

        /// <summary>
        /// 设置用户基本信息
        /// </summary>
        /// <param name="userInfo"></param>
        public static void SetBasicUserInfo(BasicUserInfo userInfo)
        {
            if (string.Equals(Appsettings.SessionType, "redis", StringComparison.OrdinalIgnoreCase))
            {
                SessionRedisUtility.SetBasicUserInfo(userInfo);
            }
            else
            {
                SessionMemoryUtility.SetBasicUserInfo(userInfo);
            }
        }

        /// <summary>
        /// 获取Session的值
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key">Session的键</param>
        /// <returns></returns>
        public static T GetSessionData<T>(string key)
        {
            if (string.Equals(Appsettings.SessionType, "redis", StringComparison.OrdinalIgnoreCase))
            {
                return SessionRedisUtility.GetSessionData<T>(key);
            }
            return SessionMemoryUtility.GetSessionData<T>(key);
        }

        /// <summary>
        /// 设置Session的值
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key">Session的键</param>
        /// <returns></returns>
        public static void SetSessionData<T>(string key, T value)
        {
            if (string.Equals(Appsettings.SessionType, "redis", StringComparison.OrdinalIgnoreCase))
            {
                SessionRedisUtility.SetSessionData(key, value);
            }
            else
            {
                SessionMemoryUtility.SetSessionData(key, value);
            }
        }

        /// <summary>
        /// 清空Session
        /// </summary>
        public static void ClearSession()
        {
            if (string.Equals(Appsettings.SessionType, "redis", StringComparison.OrdinalIgnoreCase))
            {
                SessionRedisUtility.ClearSession();
                SessionMemoryUtility.ClearSession();
            }
            else
            {
                SessionMemoryUtility.ClearSession();
            }
        }
    }
}
